﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.Common.Upload
{
    public class UpLoadFileStreamHelper
    {
        const int WRITE_FILE_SIZE = 1024 * 1024 * 2;  // 2Mb
        /// <summary>
        /// 同步上传的方法
        /// </summary>
        /// <param name="stream">文件流</param>
        /// <param name="path">物理路径</param>
        /// <returns></returns>
        public static double UploadWriteFile(Stream stream, string path)
        {
            try
            {
                double writeCount = 0;
                using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, WRITE_FILE_SIZE))
                {
                    Byte[] by = new byte[WRITE_FILE_SIZE];
                    int readCount = 0;
                    while ((readCount = stream.Read(by, 0, by.Length)) > 0)
                    {
                        fileStream.Write(by, 0, readCount);
                        writeCount += readCount;
                    }
                    return Math.Round((writeCount * 1.0 / (1024 * 1024)), 2);
                }
            }
            catch (Exception ex)
            {

                throw new Exception("发生异常：" + ex.Message);
            }
        }
        
        /// <summary>
        /// WriteFileAsync(Stream stream, string path)
        /// </summary>
        /// <param name="stream">文件流</param>
        /// <param name="path">物理路径</param>
        /// <returns></returns>
        public static async Task<double> UploadWriteFileAsync(Stream stream, string path)
        {
            try
            {
                double writeCount = 0;
                using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, WRITE_FILE_SIZE))
                {
                    Byte[] by = new byte[WRITE_FILE_SIZE];

                    int readCount = 0;
                    while ((readCount = await stream.ReadAsync(by, 0, by.Length)) > 0)
                    {
                        await fileStream.WriteAsync(by, 0, readCount);
                        writeCount += readCount;
                    }
                }
                return Math.Round((writeCount * 1.0 / (1024 * 1024)), 2);
            }
            catch (Exception ex)
            {

                throw new Exception("发生异常：" + ex.Message);
            }
        }

        /// <summary>
        /// 上传文件，需要自定义上传的路径
        /// </summary>
        /// <param name="file">文件接口对象</param>
        /// <param name="path">需要自定义上传的路径</param>
        /// <returns></returns>
        public static async Task<bool> UploadWriteFileAsync(IFormFile file, string path)
        {
            try
            {
                using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, WRITE_FILE_SIZE))
                {
                    await file.CopyToAsync(fileStream);
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("发生异常：" + ex.Message);
            }
        }
    }


}
